package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.AgentUserStatisticsBo;
import com.joker.orange.domain.vo.AgentUserStatisticsVo;
import com.joker.orange.domain.AgentUserStatistics;
import com.joker.orange.mapper.AgentUserStatisticsMapper;
import com.joker.orange.service.IAgentUserStatisticsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户统计Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class AgentUserStatisticsServiceImpl implements IAgentUserStatisticsService {

    private final AgentUserStatisticsMapper baseMapper;

    /**
     * 查询用户统计
     */
    @Override
    public AgentUserStatisticsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户统计列表
     */
    @Override
    public TableDataInfo<AgentUserStatisticsVo> queryPageList(AgentUserStatisticsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AgentUserStatistics> lqw = buildQueryWrapper(bo);
        Page<AgentUserStatisticsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户统计列表
     */
    @Override
    public List<AgentUserStatisticsVo> queryList(AgentUserStatisticsBo bo) {
        LambdaQueryWrapper<AgentUserStatistics> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AgentUserStatistics> buildQueryWrapper(AgentUserStatisticsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AgentUserStatistics> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAgentId() != null, AgentUserStatistics::getAgentId, bo.getAgentId());
        lqw.like(StringUtils.isNotBlank(bo.getAgentName()), AgentUserStatistics::getAgentName, bo.getAgentName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), AgentUserStatistics::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getUserType()), AgentUserStatistics::getUserType, bo.getUserType());
        lqw.eq(StringUtils.isNotBlank(bo.getAgentLevel()), AgentUserStatistics::getAgentLevel, bo.getAgentLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getSuperiorAgent()), AgentUserStatistics::getSuperiorAgent, bo.getSuperiorAgent());
        lqw.eq(bo.getCommissionRatio() != null, AgentUserStatistics::getCommissionRatio, bo.getCommissionRatio());
        lqw.eq(bo.getCommissionAmount() != null, AgentUserStatistics::getCommissionAmount, bo.getCommissionAmount());
        lqw.eq(bo.getAndroidRechargeAmount() != null, AgentUserStatistics::getAndroidRechargeAmount, bo.getAndroidRechargeAmount());
        lqw.eq(bo.getIosRechargeAmount() != null, AgentUserStatistics::getIosRechargeAmount, bo.getIosRechargeAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getAndroidRegistrationCount()), AgentUserStatistics::getAndroidRegistrationCount, bo.getAndroidRegistrationCount());
        lqw.eq(StringUtils.isNotBlank(bo.getIosRegistrationCount()), AgentUserStatistics::getIosRegistrationCount, bo.getIosRegistrationCount());
        lqw.eq(StringUtils.isNotBlank(bo.getMemberCount()), AgentUserStatistics::getMemberCount, bo.getMemberCount());
        lqw.eq(bo.getRechargeAmount() != null, AgentUserStatistics::getRechargeAmount, bo.getRechargeAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getDailyActiveCount()), AgentUserStatistics::getDailyActiveCount, bo.getDailyActiveCount());
        lqw.eq(StringUtils.isNotBlank(bo.getPromotionUserCount()), AgentUserStatistics::getPromotionUserCount, bo.getPromotionUserCount());
        lqw.eq(StringUtils.isNotBlank(bo.getAndroidOrderDeductionCount()), AgentUserStatistics::getAndroidOrderDeductionCount, bo.getAndroidOrderDeductionCount());
        lqw.eq(StringUtils.isNotBlank(bo.getIosOrderDeductionCount()), AgentUserStatistics::getIosOrderDeductionCount, bo.getIosOrderDeductionCount());
        lqw.eq(bo.getAndroidOrderDeductionAmount() != null, AgentUserStatistics::getAndroidOrderDeductionAmount, bo.getAndroidOrderDeductionAmount());
        lqw.eq(bo.getIosOrderDeductionAmount() != null, AgentUserStatistics::getIosOrderDeductionAmount, bo.getIosOrderDeductionAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getAndroidRegistrationDeductionCount()), AgentUserStatistics::getAndroidRegistrationDeductionCount, bo.getAndroidRegistrationDeductionCount());
        lqw.eq(StringUtils.isNotBlank(bo.getIosRegistrationDeductionCount()), AgentUserStatistics::getIosRegistrationDeductionCount, bo.getIosRegistrationDeductionCount());
        return lqw;
    }

    /**
     * 新增用户统计
     */
    @Override
    public Boolean insertByBo(AgentUserStatisticsBo bo) {
        AgentUserStatistics add = BeanUtil.toBean(bo, AgentUserStatistics.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户统计
     */
    @Override
    public Boolean updateByBo(AgentUserStatisticsBo bo) {
        AgentUserStatistics update = BeanUtil.toBean(bo, AgentUserStatistics.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AgentUserStatistics entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户统计
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
